package com.huilai.platform.country_platform.service;

import com.huilai.platform.country_platform.core.AuditEnum2;
import com.huilai.platform.country_platform.core.TypeEnum;
import com.huilai.platform.country_platform.dto.FeedbackDTO;
import com.huilai.platform.country_platform.dto.FeedbackResultDTO;
import com.huilai.platform.country_platform.model.*;
import com.huilai.platform.country_platform.repository.*;
import com.huilai.platform.country_platform.util.DateUtil;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/3/3
 * 注释：
 */
@Service
public class VillageService {

    @Autowired
    private FeedbackPersonReposity feedbackPersonReposity;

    @Autowired
    private FeedbackBeReposity feedbackBeReposity;

    @Autowired
    private VillageFeedbackReposity villageFeedbackReposity;

    @Autowired
    private VillageCommitteeReposity villageCommitteeReposity;

    @Autowired
    private VillageActivitiesReposity villageActivitiesReposity;

    @Autowired
    private UserFeedbackReposity userFeedbackReposity;

    public Integer addFeedbackPerson(FeedbackPerson feedbackPerson){
//        乡务公开-民事直说业务
        Integer integer = feedbackPersonReposity.addFeedbackPerson(feedbackPerson);
        return integer;
    }

    public Integer addFeedbackBe(FeedbackBe feedbackBe){
        Integer integer = feedbackBeReposity.addFeedbackBe(feedbackBe);
        return integer;
    }

    public Integer addVillageFeedback(VillageFeedback villageFeedback){
        Integer integer = villageFeedbackReposity.addFeedback(villageFeedback);
        return integer;
    }

    public Integer addUserFeedback(UserFeedback userFeedback){
        return userFeedbackReposity.addUserFeedback(userFeedback);
    }

    public VillageFeedback findVillageFeedbackByContentTitle(String contentTitle){
        return villageFeedbackReposity.findVillageFeedbackByContentTitle(contentTitle);
    }
    public Integer updateFeedbackPerson(FeedbackPerson feedbackPerson){
        return feedbackPersonReposity.updateFeedbackPerson(feedbackPerson);
    }

    public FeedbackPerson findFeedbackPersonByNameAndPostcard(String name, String postcard){
        return feedbackPersonReposity.findFeedbackPersonByNameAndPostcard(name, postcard);
    }

    public FeedbackBe findFeedbackBeByNameAndUnitAndPositionAndLevel(String name, String unit, String position, String level){
        return feedbackBeReposity.findFeedbackBeByNameAndUnitAndPositionAndLevel(name, unit, position, level);
    }
    public UserFeedback findUserFeedbackByFeedbackId(Integer feedbackId){
        return userFeedbackReposity.findUserFeedbackByFeedbackId(feedbackId);
    }

    public Integer updateUserFeedback(Integer id, String describe){
        return userFeedbackReposity.updateUserFeedback(id, describe);
    }

    public Integer updateUserFeedback2(Integer id, String endDescribe){
        return userFeedbackReposity.updateUserFeedback2(id, endDescribe);
    }

    public Integer changeFeedbackAuditStatusById(Integer id, Integer auditSatus){
        Integer integer = villageFeedbackReposity.updateAuditStatus(id, auditSatus);
        return integer;
    }

    public FeedbackDTO getVillageFeedbackById(Integer id){
        VillageFeedback villageFeedback = villageFeedbackReposity.getOne(id);
        FeedbackPerson feedbackPerson = null;
        FeedbackBe feedbackBe = null;
        if (villageFeedback.getFeedbackId()!=null){
            feedbackPerson = feedbackPersonReposity.getOne(villageFeedback.getFeedbackId());
            if (villageFeedback.getBeId()!=null){
                feedbackBe = feedbackBeReposity.getOne(villageFeedback.getBeId());
                FeedbackDTO feedbackDTO = new FeedbackDTO(
                        id,
                        feedbackPerson.getName(),
                        feedbackPerson.getPostcard(),
                        feedbackPerson.getTelephone(),
                        feedbackPerson.getPolitical(),
                        feedbackPerson.getAddress(),
                        feedbackPerson.getPosition(),
                        feedbackPerson.getComplaintCategory(),
                        feedbackPerson.getComplaintSituation(),
                        feedbackBe.getName(),
                        feedbackBe.getUnit(),
                        feedbackBe.getPosition(),
                        feedbackBe.getLevel(),
                        villageFeedback.getContentTitle(),
                        villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                        villageFeedback.getContentMain(),
                        AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                        TypeEnum.getStringByStatus(villageFeedback.getType())
                );
                return feedbackDTO;
            }else{
                FeedbackDTO feedbackDTO = new FeedbackDTO(
                        id,
                        feedbackPerson.getName(),
                        feedbackPerson.getPostcard(),
                        feedbackPerson.getTelephone(),
                        feedbackPerson.getPolitical(),
                        feedbackPerson.getAddress(),
                        feedbackPerson.getPosition(),
                        feedbackPerson.getComplaintCategory(),
                        feedbackPerson.getComplaintSituation(),
                        null,
                        null,
                        null,
                        null,
                        villageFeedback.getContentTitle(),
                        villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                        villageFeedback.getContentMain(),
                        AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                        TypeEnum.getStringByStatus(villageFeedback.getType())
                );
                return feedbackDTO;
            }
        }
        else {
            if (villageFeedback.getBeId()!=null){
                feedbackBe = feedbackBeReposity.getOne(villageFeedback.getBeId());
                FeedbackDTO feedbackDTO = new FeedbackDTO(
                        id,
                        "---",
                        "未填写",
                        "未填写",
                        "未填写",
                        "未填写",
                        "未填写",
                        null,
                        null,
                        feedbackBe.getName(),
                        feedbackBe.getUnit(),
                        feedbackBe.getPosition(),
                        feedbackBe.getLevel(),
                        villageFeedback.getContentTitle(),
                        villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                        villageFeedback.getContentMain(),
                        AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                        TypeEnum.getStringByStatus(villageFeedback.getType())
                );
                return feedbackDTO;
            }else{
                FeedbackDTO feedbackDTO = new FeedbackDTO(
                        id,
                        "---",
                        "未填写",
                        "未填写",
                        "未填写",
                        "未填写",
                        "未填写",
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        villageFeedback.getContentTitle(),
                        villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                        villageFeedback.getContentMain(),
                        AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                        TypeEnum.getStringByStatus(villageFeedback.getType())
                );
                return feedbackDTO;
            }
        }
    }

    public Page<FeedbackResultDTO> toVillageFeedbackListUI(Pageable pageable){
        Page<VillageFeedback> villageFeedbackPage = villageFeedbackReposity.findAll((root, query, cb) ->{
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.or(
                    (cb.equal(root.get("auditStatus"), "1")),
                    (cb.equal(root.get("auditStatus"), "2"))));

            query.where(predicates.toArray(new Predicate[]{}));
            query.orderBy(cb.desc(root.get("updateTime")));
            return null;
        },pageable);
        Page<FeedbackResultDTO> feedbackResultDTOPage = villageFeedbackPage.map(villageFeedback -> {
            UserFeedback userFeedback = userFeedbackReposity.findUserFeedbackByFeedbackId(villageFeedback.getId());
            FeedbackPerson feedbackPerson = null;
            FeedbackBe feedbackBe = null;
            if (villageFeedback.getFeedbackId()!=null){
                feedbackPerson = feedbackPersonReposity.getOne(villageFeedback.getFeedbackId());
                if (villageFeedback.getBeId()!=null){
                    feedbackBe = feedbackBeReposity.getOne(villageFeedback.getBeId());
                    FeedbackResultDTO feedbackResultDTO = new FeedbackResultDTO(
                            villageFeedback.getId(),
                            feedbackPerson.getName(),
                            feedbackPerson.getPostcard(),
                            feedbackPerson.getTelephone(),
                            feedbackPerson.getPolitical(),
                            feedbackPerson.getAddress(),
                            feedbackPerson.getPosition(),
                            feedbackPerson.getComplaintCategory(),
                            feedbackPerson.getComplaintSituation(),
                            feedbackBe.getName(),
                            feedbackBe.getUnit(),
                            feedbackBe.getPosition(),
                            feedbackBe.getLevel(),
                            villageFeedback.getContentTitle(),
                            villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                            villageFeedback.getContentMain(),
                            AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                            TypeEnum.getStringByStatus(villageFeedback.getType()),
                            userFeedback.getDealDescribe(),
                            userFeedback.getEndDescribe(),
                            userFeedback.getUnread(),
                            DateUtil.fromDateToString(villageFeedback.getCreateTime()),
                            DateUtil.fromDateToString(villageFeedback.getUpdateTime())
                    );
                    return feedbackResultDTO;
                }else{
                    FeedbackResultDTO feedbackResultDTO = new FeedbackResultDTO(
                            villageFeedback.getId(),
                            feedbackPerson.getName(),
                            feedbackPerson.getPostcard(),
                            feedbackPerson.getTelephone(),
                            feedbackPerson.getPolitical(),
                            feedbackPerson.getAddress(),
                            feedbackPerson.getPosition(),
                            feedbackPerson.getComplaintCategory(),
                            feedbackPerson.getComplaintSituation(),
                            null,
                            null,
                            null,
                            null,
                            villageFeedback.getContentTitle(),
                            villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                            villageFeedback.getContentMain(),
                            AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                            TypeEnum.getStringByStatus(villageFeedback.getType()),
                            userFeedback.getDealDescribe(),
                            userFeedback.getEndDescribe(),
                            userFeedback.getUnread(),
                            DateUtil.fromDateToString(villageFeedback.getCreateTime()),
                            DateUtil.fromDateToString(villageFeedback.getUpdateTime())
                    );
                    return feedbackResultDTO;
                }
            }
            else {
                if (villageFeedback.getBeId()!=null){
                    feedbackBe = feedbackBeReposity.getOne(villageFeedback.getBeId());
                    FeedbackResultDTO feedbackResultDTO = new FeedbackResultDTO(
                            villageFeedback.getId(),
                            "---",
                            "未填写",
                            "未填写",
                            "未填写",
                            "未填写",
                            "未填写",
                            null,
                            null,
                            feedbackBe.getName(),
                            feedbackBe.getUnit(),
                            feedbackBe.getPosition(),
                            feedbackBe.getLevel(),
                            villageFeedback.getContentTitle(),
                            villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                            villageFeedback.getContentMain(),
                            AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                            TypeEnum.getStringByStatus(villageFeedback.getType()),
                            userFeedback.getDealDescribe(),
                            userFeedback.getEndDescribe(),
                            userFeedback.getUnread(),
                            DateUtil.fromDateToString(villageFeedback.getCreateTime()),
                            DateUtil.fromDateToString(villageFeedback.getUpdateTime())
                    );
                    return feedbackResultDTO;
                }else{
                    FeedbackResultDTO feedbackResultDTO = new FeedbackResultDTO(
                            villageFeedback.getId(),
                            "---",
                            "未填写",
                            "未填写",
                            "未填写",
                            "未填写",
                            "未填写",
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            villageFeedback.getContentTitle(),
                            villageFeedback.getContentCategory() == null?"群众反馈":villageFeedback.getContentCategory(),
                            villageFeedback.getContentMain(),
                            AuditEnum2.getStringByStatus(villageFeedback.getAuditStatus()),
                            TypeEnum.getStringByStatus(villageFeedback.getType()),
                            userFeedback.getDealDescribe(),
                            userFeedback.getEndDescribe(),
                            userFeedback.getUnread(),
                            DateUtil.fromDateToString(villageFeedback.getCreateTime()),
                            DateUtil.fromDateToString(villageFeedback.getUpdateTime())
                    );
                    return feedbackResultDTO;
                }
            }
        });
        return feedbackResultDTOPage;

    }

    public List<VillageCommittee> committeeUI(String searchName, String unit){
//        乡务公开-乡委管理等业务
        List<VillageCommittee> villageCommitteeList = villageCommitteeReposity.findAll((root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!StringUtils.isEmpty(searchName)) {
                predicates.add(cb.or(
                        (cb.like(root.get("name"), "%" + searchName + "%")),
                        (cb.like(root.get("position"), "%" + searchName + "%"))));
            }
            predicates.add(cb.equal(root.get("unit"),unit));
            query.where(predicates.toArray(new Predicate[]{}));
            return null;
        });
        return villageCommitteeList;

    }

    public Integer addVillageCommittee(VillageCommittee villageCommittee){
        if (villageCommittee.getImage().equals("example")){
            villageCommittee.setImage(null);
        }
        Integer integer = villageCommitteeReposity.addVillageCommittee(villageCommittee);
        return integer;
    }

    public VillageCommittee getCommitteeById(Integer id){
        VillageCommittee villageCommittee = villageCommitteeReposity.getOne(id);
        return villageCommittee;
    }

    public Integer updateCommittee(VillageCommittee villageCommittee){
        if (villageCommittee.getImage().equals("example")){
            villageCommittee.setImage(null);
        }
        Integer integer = villageCommitteeReposity.updateCommittee(villageCommittee);
        return integer;
    }

    public Integer deleteCommitteeById(Integer id){
        Integer integer = villageCommitteeReposity.deleteCommitteeById(id);
        return integer;
    }

    public VillageCommittee findCommitteeById(Integer id){
        return villageCommitteeReposity.findById(id).orElseThrow(() -> new ServiceException("党务管理id错误!"));
    }

    public Page<VillageActivities> activitiesUI(String searchName, String contentType, Pageable pageable) {
        Page<VillageActivities> buildingPage = villageActivitiesReposity.findAll((root, query, cb) -> {
                    List<Predicate> predicates = new ArrayList<>();
                    //模糊查询
                    if (!StringUtils.isEmpty(searchName)) {
                        predicates.add((cb.like(root.get("title"), "%" + searchName + "%")));
                    }
                    predicates.add(cb.equal(root.get("contentType"),contentType));
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                , pageable);
        return buildingPage;
    }

    public List<VillageActivities> getVillageActivitiesList(String contentType){
        PageRequest pageable = PageRequest.of(0, 7);
        Page<VillageActivities> villageActivitiesPage = villageActivitiesReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    predicates.add(cb.equal(root.get("contentType"),contentType));
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                ,pageable);
        return villageActivitiesPage.getContent();
    }

    public Integer addVillageActivities(VillageActivities villageActivities){
        if (villageActivities.getImage().equals("example")){
            villageActivities.setImage(null);
        }
        if (villageActivities.getFile().equals("example")){
            villageActivities.setFile(null);
        }
        Integer integer = villageActivitiesReposity.addVillageActivities(villageActivities);
        return integer;
    }
    public VillageActivities getVillageActivitiesById(Integer id){
        VillageActivities villageActivities = villageActivitiesReposity.getOne(id);
        return villageActivities;
    }

    public Integer updateVillageActivities(VillageActivities villageActivities){
        if (villageActivities.getImage().equals("example")){
            villageActivities.setImage(null);
        }
        if (villageActivities.getFile().equals("example")){
            villageActivities.setFile(null);
        }
        Integer integer = villageActivitiesReposity.updateVillageActivities(villageActivities);
        return integer;
    }

    public Integer deleteVillageActivitiesById(Integer id){
        Integer integer = villageActivitiesReposity.deleteVillageActivitiesById(id);
        return integer;
    }

    public VillageActivities findActivitiesyId(Integer id){
        return villageActivitiesReposity.findById(id).orElseThrow(() -> new ServiceException("公开事项id错误!"));
    }

    public List<VillageActivities> getTopActivities(String contentType){
        PageRequest pageable = PageRequest.of(0, 3);
        Page<VillageActivities> buildingPage = villageActivitiesReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    predicates.add(cb.equal(root.get("contentType"),contentType));
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                },pageable);
        return buildingPage.getContent();
    }

    public List<VillageCommittee> getvillageCommitteeList(){
//        筛选所有副镇长
        PageRequest pageable = PageRequest.of(0, 6);
        Page<VillageCommittee> villageCommitteePage = villageCommitteeReposity.findAll((root, query, cb) ->{
            List<Predicate> predicates = new ArrayList<>();
            predicates.add((cb.like(root.get("position"), "%" + "副镇长" + "%")));
            query.where(predicates.toArray(new Predicate[]{}));
            query.orderBy(cb.desc(root.get("name")));
            return null;
        },pageable);
        return villageCommitteePage.getContent();
    }

    public List<VillageCommittee> getvillageCommitteeList2(){
//        筛选所有镇委副书记
        PageRequest pageable = PageRequest.of(0, 6);
        Page<VillageCommittee> villageCommitteePage = villageCommitteeReposity.findAll((root, query, cb) ->{
            List<Predicate> predicates = new ArrayList<>();
            predicates.add((cb.like(root.get("position"), "%" + "镇委副书记" + "%")));
            query.where(predicates.toArray(new Predicate[]{}));
            query.orderBy(cb.desc(root.get("name")));
            return null;
        },pageable);
        return villageCommitteePage.getContent();
    }

    public VillageCommittee getZhenzhang(){
//        找到唯一的镇长
        VillageCommittee villageCommittee = villageCommitteeReposity.findVillageCommitteeByPosition("镇长、镇委副书记");
        return villageCommittee;
    }

    public VillageCommittee getShuji(){
//        找到唯一的镇长
        VillageCommittee villageCommittee = villageCommitteeReposity.findVillageCommitteeByPosition("镇委书记");
        return villageCommittee;
    }
}
